package com.example.community.add;

import com.example.community.dao.FloorDao;
import com.example.community.dao.UnitDao;
import com.example.community.model.Floor;
import com.example.community.model.Unit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/unitAddServlet")
public class UnitAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        FloorDao floorDao = new FloorDao();
        List<Floor> floors = floorDao.fandByAll();
        req.setAttribute("floors",floors);
        req.getRequestDispatcher("/WEB-INF/houseManager/unitAdd.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String uname = req.getParameter("uname");
            String fno = req.getParameter("fno");

            Unit unit = new Unit();
            unit.setUnName(uname);
            unit.setfNo(Integer.parseInt(fno));

            UnitDao unitDao = new UnitDao();
            Integer insert = unitDao.insert(unit);
            if (insert == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }catch (Exception e){
            resp.getWriter().write("false");
        }

    }
}
